package com.example.sharesinfo.manager;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.example.sharesinfo.bean.PersonalCenterBean;
import com.example.sharesinfo.bean.TalkBean;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.helper.UIHelper;
import com.example.sharesinfo.model.TalkModel;
import com.example.sharesinfo.ui.PersonalCenterActivity;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.example.sharesinfo.utils.SpUtils;
import com.google.gson.Gson;

import org.json.JSONException;

public class UserManager {
    private static final int QUERY_USER_CODE = 1001;
    //对象实例化
    private static volatile UserManager mInstance = null;
    private Context mContext;


    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case QUERY_USER_CODE:
                    PersonalCenterBean result1 = (PersonalCenterBean) msg.obj;
                    PersonalCenterActivity.startActivity(mContext, result1, false);
                    break;
                case 404:
                    break;
            }
            return false;
        }
    });

    private UserManager() {

    }

    public static UserManager getInstance() {
        if (mInstance == null) {
            synchronized (DialogManager.class) {
                if (mInstance == null) {
                    mInstance = new UserManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 跳转用户主页
     */
    public void queryUserInfo(int userId, Context context) {
        mContext = context;
        String url = "http://119.23.221.142/api/user/GetHomePage?UserId=" + userId
                + "&MyId=" + SpUtils.getInstance().getString(Constants.SP_USER_ID, "");
        LogUtils.i(url);
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {
            Message message = mHandler.obtainMessage();

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                //LogUtils.i(LOGTAG + s);
                PersonalCenterBean bean = new Gson().fromJson(s, PersonalCenterBean.class);
                message.what = QUERY_USER_CODE;
                message.obj = bean;
                mHandler.sendMessage(message);
            }

            @Override
            public void OnFailed(String s) {
                message.what = 404;
                message.obj = s;
                mHandler.sendMessage(message);
            }
        });
    }
}
